package com.ruoyi.project.module.whd.kaipai;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.project.domain.*;
import com.ruoyi.project.service.*;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.InetSocketAddress;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static com.ruoyi.common.security.utils.SecurityUtils.getLoginUser;
import java.util.Scanner;
import java.net.Socket;
/**
 * 吧台账单Controller
 *
 * @author dr
 * @date 2023-04-17
 */
@RestController
@RequestMapping("/whd/kaipai")
public class WhdBarBillsController extends BaseController
{
    @Autowired
    private IYqgBarBillsService yqgBarBillsService;
    @Autowired
    private IYqgHandsService yqgHandsService;
    @Autowired
    private IYqgConsumptionDetailsService yqgConsumptionDetailsService;
    @Autowired
    private IYqgInformationProjectService yqgInformationProjectService;
    @Autowired
    private IYqgItemAssociationService yqgItemAssociationService;

    /**
     * 查询吧台账单列表
     */
   /* @RequiresPermissions("project:whd:kaipai:list")*/
    @GetMapping("/list")
    public TableDataInfo list(YqgBarBills yqgBarBills)
    {
        startPage();
        List<YqgBarBills> list = yqgBarBillsService.selectYqgBarBillsList(yqgBarBills);
        return getDataTable(list);
    }

    /**
     * 导出吧台账单列表
     */
   /* @RequiresPermissions("project:whd:kaipai:export")*/
    @Log(title = "吧台账单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YqgBarBills yqgBarBills)
    {
        List<YqgBarBills> list = yqgBarBillsService.selectYqgBarBillsList(yqgBarBills);
        ExcelUtil<YqgBarBills> util = new ExcelUtil<YqgBarBills>(YqgBarBills.class);
        util.exportExcel(response, list, "吧台账单数据");
    }
    /**
     * 获取手牌基础信息详细信息
     */
    /* @RequiresPermissions("project:whd:kaipai:query")*/
    @GetMapping(value = "/hand/{HandId}")
    public YqgHands getHandBrand(@PathVariable("HandId") String HandId)
    {
        YqgHands yqgHands=new YqgHands();
        yqgHands.setHandId(HandId);
        List<YqgHands> list = yqgHandsService.selectYqgHandsList(yqgHands);
        if(list.size()>0){
            return yqgHandsService.selectYqgHandsByHandId(HandId);
        }
        else{
            yqgHands.setHandBrand("");
            return yqgHands;
        }

    }
    /**
     * 获取手牌基础信息详细信息
     */
    /* @RequiresPermissions("project:whd:kaipai:query")*/
    @GetMapping(value = "/BarBill/{HandBrand}")
    public YqgBarBills getMastercard(@PathVariable("HandBrand") String HandBrand)
    {
        YqgBarBills yqgBarBills=new YqgBarBills();
        yqgBarBills.setHandBrand(HandBrand);
        yqgBarBills.setHandState("1");
        List<YqgBarBills> list = yqgBarBillsService.selectYqgBarBillsList(yqgBarBills);
        if(list.size()>0){
            return list.get(0);
        }
        else{
            yqgBarBills.setMastercard("");
            return yqgBarBills;
        }

    }


    /**
     * 获取手牌基础信息详细信息
     */
   /* @RequiresPermissions("project:whd:kaipai:query")*/
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {

        if(id==null&&id.equals("")){
            return new AjaxResult(500," 手牌号不能为空,请读牌后使用！");
        }
        YqgHands hands=new YqgHands();
        hands.setHandBrand(id);
        /*hands.setHandState("0");
        hands.setLockedState("0");*/
        List<YqgHands> list = yqgHandsService.selectYqgHandsList(hands);
        if(list.size()!=0){
            YqgBarBills yqgBarBills=new YqgBarBills();
            yqgBarBills.setHandBrand(id);
            yqgBarBills.setHandState("1");
            List<YqgBarBills> blist=yqgBarBillsService.selectYqgBarBillsList(yqgBarBills);
            if(blist.size()==0){
                return  new AjaxResult(200,id+"开牌成功");
            }
            else{
                return  new AjaxResult(500,id+"手牌已在台");
            }

        }else{
            return  new AjaxResult(500,"请输入正确的手牌号");
        }
    }
    /**
     * 新增吧台账单
     */

   /* @RequiresPermissions("project:whd:kaipai:add")*/
    @Log(title="开牌",businessType = BusinessType.INSERT,logicalKey = "handBrand")
    @PostMapping
    public AjaxResult kaipai(@RequestBody YqgBarBills yqgBarBills)
    {
        String together=yqgBarBills.getTogether();
        String[] tlist1=together.split(",");
        //主牌对象
        YqgBarBills BarBills=new YqgBarBills();
        BarBills.setHandBrand(yqgBarBills.getMastercard());
        BarBills.setHandState("1");
        List<YqgBarBills> list = yqgBarBillsService.selectYqgBarBillsList(BarBills);
        if(list.size()>0){
            //补加开牌
            BarBills=list.get(0);
            //是否录入手牌
            if(BarBills.getTogether().equals(together)){
                return new AjaxResult(500," 手牌号不能为空,请读牌后使用！");
            }else{
                String[] tlist=BarBills.getTogether().split(",");
                //更新已录入手牌信息
                for(int i=0;i<tlist.length;i++){
                    //更新对象
                    YqgBarBills BarBill=new YqgBarBills();
                    BarBill.setHandBrand(tlist[i]);
                    BarBill.setHandState("1");
                    List<YqgBarBills> barlist = yqgBarBillsService.selectYqgBarBillsList(BarBill);
                    BarBill=barlist.get(0);
                    //消费详细对象
                    YqgConsumptionDetails yqgConsumptionDetails=new YqgConsumptionDetails();
                    yqgConsumptionDetails.setBarBillsId(BarBill.getID());
                    List<YqgConsumptionDetails> ConsumptionList = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);

                    //更新消费详细表
                    for (YqgConsumptionDetails ConsumptionDetails:ConsumptionList) {
                        ConsumptionDetails.setTogether(together);
                        yqgConsumptionDetailsService.updateYqgConsumptionDetails(ConsumptionDetails);
                    }
                    BarBill.setTogether(together);
                    BarBill.setNumberOfPeople(String.valueOf(tlist1.length));
                    yqgBarBillsService.updateYqgBarBills(BarBill);
                }
                //剔除已存在的手牌
                together=together.replace(BarBills.getTogether()+",","");
                //插入未开台的手牌
                return add(yqgBarBills,together);
            }

        }
        else{
            //第一次开牌
            return add(yqgBarBills,together);
        }

    }
    public AjaxResult add(YqgBarBills yqgBarBills,String together)
    {
        if(yqgBarBills.getTogether()==null){
            return new AjaxResult(500," 手牌号不能为空,请读牌后使用！");
        }
        /*if(yqgBarBills.getNote1()!=null){
            System.out.println(yqgBarBills.getNote1());
            System.out.println(yqgBarBills.getNote2());

            for(int i=0;i< yuzi.length;i++){
                System.out.println(yuzi[i]);
                System.out.println(ertong[i]);
            }


        }*/
        /*String[] yuzi=yqgBarBills.getNote1().split(",");
        String[] ertong=yqgBarBills.getNote2().split(",");
        yqgBarBills.setNote1(null);
        yqgBarBills.setNote2(null);*/


        String mastercard=yqgBarBills.getMastercard();

        /*if(together==null||together.equals("")){
            together=yqgBarBills.getHandBrand()+",";
        }
        else{
            together=yqgBarBills.getHandBrand()+","+together;
            yqgBarBills.setTogether(together);
        }*/
        //控制插入手牌数量
        String[] tlist=together.split(",");
        //控制同来人数
        String[] tlist1=yqgBarBills.getTogether().split(",");
        String Together=yqgBarBills.getTogether();
        //控制套餐数量
        String[] taocanlist=yqgBarBills.getNote1().split(",");
        yqgBarBills.setNote1(null);






        /*String[] tlist= Arrays.copyOf(togetherlist,togetherlist.length+1);
        tlist[tlist.length-1]=yqgBarBills.getHandBrand();*/
        yqgBarBills.setNumberOfPeople(String.valueOf(tlist1.length));
        String openingTime=DateUtils.getTime();
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        //服务员信息
        yqgBarBills.setUserId(sysUser.getUserId());
        yqgBarBills.setDeptId(sysUser.getDeptId());
        yqgBarBills.setNickName(sysUser.getNickName());
        yqgBarBills.setUserName(sysUser.getUserName());
        yqgBarBills.setOperator(sysUser.getUserName());
        yqgBarBills.setCardiner(sysUser.getUserName());
        //职务
        yqgBarBills.setPost(sysUser.getRemark());
        //吧台信息
        //过夜费
       /* YqgConsumptionDetails gyConsumptionDetails=new YqgConsumptionDetails();
        gyConsumptionDetails.setUserId(sysUser.getUserId());
        gyConsumptionDetails.setDeptId(sysUser.getDeptId());
        gyConsumptionDetails.setNickName(sysUser.getNickName());
        gyConsumptionDetails.setUserName(sysUser.getUserName());
        gyConsumptionDetails.setDepartment("3");*/

        /*gyConsumptionDetails.setRetentionStatus("1");
        gyConsumptionDetails.setMergeStatus("1");
        gyConsumptionDetails.setHandState("1");*/

       /* gyConsumptionDetails.setOpeningTime(DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",openingTime));*/
        /*YqgInformationProject gyproject=new YqgInformationProject();
        gyproject.setProjectCode("00008");
        List<YqgInformationProject> gylist = yqgInformationProjectService.selectYqgInformationProjectList(gyproject);
        gyproject=gylist.get(0);*/
     /*   gyConsumptionDetails.setProjectCode(gyproject.getProjectCode());
        gyConsumptionDetails.setProjectCategory(gyproject.getProjectCategory());
        gyConsumptionDetails.setEntryName(gyproject.getEntryName());
        gyConsumptionDetails.setMnemonicCode(gyproject.getMnemonicCode());
        gyConsumptionDetails.setUnit(gyproject.getUnit());
        gyConsumptionDetails.setUnitPrice(gyproject.getUnitPrice());
        gyConsumptionDetails.setCommissionPrice(gyproject.getCommissionPrice());

        gyConsumptionDetails.setQuantity("1");
        gyConsumptionDetails.setConsumptionStatus("0");
        gyConsumptionDetails.setTotalAmount(gyproject.getUnitPrice());
        gyConsumptionDetails.setRecordingTime(DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",openingTime));*/
        yqgBarBills.setBarNumber("6");
        yqgBarBills.setOpeningTime(openingTime);
        YqgConsumptionDetails yqgConsumptionDetails=new YqgConsumptionDetails();
        //门票
        yqgConsumptionDetails.setUserId(sysUser.getUserId());
        yqgConsumptionDetails.setDeptId(sysUser.getDeptId());
        yqgConsumptionDetails.setNickName(sysUser.getNickName());
        yqgConsumptionDetails.setUserName(sysUser.getUserName());
        yqgConsumptionDetails.setDepartment("3");
        yqgConsumptionDetails.setPost(sysUser.getRemark());


        yqgConsumptionDetails.setRetentionStatus("1");
        yqgConsumptionDetails.setMergeStatus("1");
        yqgConsumptionDetails.setHandState("1");

        yqgConsumptionDetails.setOpeningTime(DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",openingTime));
        yqgConsumptionDetails.setProjectCategory("01");

        yqgConsumptionDetails.setMnemonicCode("mp");
        yqgConsumptionDetails.setUnit("张");

        yqgConsumptionDetails.setQuantity("1");
        yqgConsumptionDetails.setConsumptionStatus("0");

        yqgConsumptionDetails.setRecordingTime(DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",openingTime));
        //套餐对象
        YqgConsumptionDetails TaoCanConsumptionDetails=new YqgConsumptionDetails();
        TaoCanConsumptionDetails.setUserId(sysUser.getUserId());
        TaoCanConsumptionDetails.setDeptId(sysUser.getDeptId());
        TaoCanConsumptionDetails.setNickName(sysUser.getNickName());
        TaoCanConsumptionDetails.setUserName(sysUser.getUserName());
        TaoCanConsumptionDetails.setDepartment("3");
        TaoCanConsumptionDetails.setPost(sysUser.getRemark());

        TaoCanConsumptionDetails.setRetentionStatus("1");
        TaoCanConsumptionDetails.setMergeStatus("1");
        TaoCanConsumptionDetails.setHandState("1");

        TaoCanConsumptionDetails.setConsumptionStatus("0");
        TaoCanConsumptionDetails.setOpeningTime(DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",openingTime));
        TaoCanConsumptionDetails.setRecordingTime(DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",openingTime));

        int total;
        for(int i=0;i<tlist.length;i++){
            int flag=1;
            //费用计算
            total=0;
            //手牌信息
            // 判断输入手牌号
            YqgHands yqgHands=new YqgHands();
            yqgHands.setHandBrand(tlist[i]);
            //此处向泰格锁发消息
           /* flag=sendmessage(tlist[i]);
            if(flag==0){
                return new AjaxResult(500," 泰格锁设备连接超时！请检查设备是否正常");
            }*/
            List<YqgHands> list = yqgHandsService.selectYqgHandsList(yqgHands);

            /*if(list.size()==0){
                return new AjaxResult(500,tlist[i]+" 手牌号输入错误");
            }
            else{
                YqgBarBills ybb=new YqgBarBills();
                ybb.setHandBrand(tlist[i]);
                ybb.setHandState("1");
                List<YqgBarBills> elist = yqgBarBillsService.selectYqgBarBillsList(ybb);

                if(elist.size()!=0){
                    return new AjaxResult(500,tlist[i]+" 手牌已开台");
                }
            }*/
            yqgHands=list.get(0);
            yqgBarBills.setHandBrand(yqgHands.getHandBrand());
            yqgBarBills.setHandCategory(yqgHands.getHandCategory());
            //原手牌
            yqgBarBills.setOriginalHand(yqgHands.getHandBrand());
            yqgBarBills.setMastercard(mastercard);
            yqgBarBills.setHandoverStatus("0");
            yqgBarBills.setHandState("1");
            yqgHands.setHandState("1");
            yqgHands.setLockedState("0");
            yqgBarBills.setRetentionStatus("1");
            yqgBarBills.setMergeStatus("1");
            String bdate=DateUtils.dateTimeNow("yyyy-MM-dd");
            String bai=bdate+" 18:00:00";
            //白天
            Date baidate= DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",bai);
            LocalDateTime baiTime= LocalDateTime.ofInstant(baidate.toInstant(), ZoneId.systemDefault());
            LocalDateTime currentDateTime = LocalDateTime.now();
            YqgInformationProject mpproject=new YqgInformationProject();
            if(baiTime.isAfter(currentDateTime)){
                if(yqgHands.getHandCategory().equals("0")){
                    //男浴白
                    mpproject.setEntryName("男浴白");
                }
                else if(yqgHands.getHandCategory().equals("1")){
                    //女浴白
                    mpproject.setEntryName("女浴白");
                }
                else if(yqgHands.getHandCategory().equals("2")){
                    //儿童
                    mpproject.setEntryName("儿童");
                }
                List<YqgInformationProject> mplist = yqgInformationProjectService.selectYqgInformationProjectList(mpproject);
                mpproject=mplist.get(0);

            }
            else{
                if(yqgHands.getHandCategory().equals("0")){
                    //男浴晚
                    mpproject.setEntryName("男浴晚");
                }
                else if(yqgHands.getHandCategory().equals("1")){
                    //女浴晚
                    mpproject.setEntryName("女浴晚");
                }
                else if(yqgHands.getHandCategory().equals("2")){
                    //儿童
                    mpproject.setEntryName("儿童");
                }
                List<YqgInformationProject> mplist = yqgInformationProjectService.selectYqgInformationProjectList(mpproject);
                mpproject=mplist.get(0);

            }

            /*if(ertong[i].equals("1")){
                yqgHands.setLockedState("0");
            }else{
                yqgHands.setLockedState("1");
            }*/
            yqgHands.setLockedState("1");
            yqgConsumptionDetails.setHandBrand(yqgHands.getHandBrand());
            yqgConsumptionDetails.setHandCategory(yqgHands.getHandCategory());
            yqgConsumptionDetails.setOriginalHand(yqgHands.getHandBrand());
            yqgConsumptionDetails.setTogether(Together);
            yqgConsumptionDetails.setHandoverStatus("0");

            TaoCanConsumptionDetails.setHandBrand(yqgHands.getHandBrand());
            TaoCanConsumptionDetails.setHandCategory(yqgHands.getHandCategory());
            TaoCanConsumptionDetails.setOriginalHand(yqgHands.getHandBrand());
            TaoCanConsumptionDetails.setTogether(Together);
            TaoCanConsumptionDetails.setHandoverStatus("0");

            /*gyConsumptionDetails.setHandBrand(yqgHands.getHandBrand());
            gyConsumptionDetails.setHandCategory(yqgHands.getHandCategory());
            gyConsumptionDetails.setOriginalHand(yqgHands.getHandBrand());
            gyConsumptionDetails.setTogether(together);
            gyConsumptionDetails.setHandoverStatus("0");*/
            /*yqgConsumptionDetails.setSailingsSigns(yqgBarBills.getSailings());*/
            yqgHandsService.updateYqgHands(yqgHands);
            Date current= DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",openingTime);
            String date=DateUtils.dateTimeNow("yyyy-MM-dd");
            String start=date+" 00:00:00";
            String end=date+" 06:00:00";

            Date startdate= DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",start);
            Date enddate= DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",end);
            LocalDateTime curentTime= LocalDateTime.ofInstant(current.toInstant(), ZoneId.systemDefault());
            LocalDateTime startTime= LocalDateTime.ofInstant(startdate.toInstant(), ZoneId.systemDefault());
            LocalDateTime endTime = LocalDateTime.ofInstant(enddate.toInstant(), ZoneId.systemDefault());

            if(curentTime.isAfter(startTime)&&curentTime.isBefore(endTime)&&!yqgHands.getHandCategory().equals("3")){
                /*total=total+Integer.valueOf(gyproject.getUnitPrice());*/
            }
            if(yqgHands.getHandCategory().equals("3")){

            }else{
                total=total+Integer.valueOf(mpproject.getUnitPrice());
            }
            yqgBarBills.setAllCost(String.valueOf(total));

            yqgBarBillsService.insertYqgBarBills(yqgBarBills);
            List<YqgBarBills> blist = yqgBarBillsService.selectYqgBarBillsList(yqgBarBills);


           /* if(!yqgHands.getHandCategory().equals("3")){}*/
            YqgItemAssociation yqgItemAssociation=new YqgItemAssociation();
            if(!taocanlist[i].equals("0")){
                yqgItemAssociation.setSetMealId(Long.valueOf(taocanlist[i]));
                List<YqgItemAssociation> ItemList = yqgItemAssociationService.selectYqgItemAssociationList(yqgItemAssociation);
                TaoCanConsumptionDetails.setBarBillsId(blist.get(0).getID());

                YqgInformationProject Itemproject;
                for(int j=0;j<ItemList.size();j++){
                    Itemproject=yqgInformationProjectService.selectYqgInformationProjectByID(ItemList.get(j).getProjectId());
                    TaoCanConsumptionDetails.setQuantity("1");
                    TaoCanConsumptionDetails.setUnit(Itemproject.getUnit());
                    TaoCanConsumptionDetails.setProjectCategory(Itemproject.getProjectCategory());
                    TaoCanConsumptionDetails.setProjectCode(Itemproject.getProjectCode());
                    TaoCanConsumptionDetails.setMnemonicCode(Itemproject.getMnemonicCode());
                    TaoCanConsumptionDetails.setEntryName(Itemproject.getEntryName());
                    TaoCanConsumptionDetails.setUnitPrice(Itemproject.getUnitPrice());
                    TaoCanConsumptionDetails.setTotalAmount(Itemproject.getUnitPrice());
                    TaoCanConsumptionDetails.setCommissionPrice(Itemproject.getCommissionPrice());
                    yqgConsumptionDetailsService.insertYqgConsumptionDetails(TaoCanConsumptionDetails);
                }
            }


                yqgConsumptionDetails.setBarBillsId(blist.get(0).getID());

                if(yqgHands.getHandCategory().equals("3")){

                }else {
                    yqgConsumptionDetails.setProjectCode(mpproject.getProjectCode());
                    yqgConsumptionDetails.setMnemonicCode(mpproject.getMnemonicCode());
                    yqgConsumptionDetails.setEntryName(mpproject.getEntryName());
                    yqgConsumptionDetails.setUnitPrice(mpproject.getUnitPrice());
                    yqgConsumptionDetails.setTotalAmount(mpproject.getUnitPrice());
                    yqgConsumptionDetails.setCommissionPrice(mpproject.getCommissionPrice());
                    yqgConsumptionDetailsService.insertYqgConsumptionDetails(yqgConsumptionDetails);
                }
            if(curentTime.isAfter(startTime)&&curentTime.isBefore(endTime)&&!yqgHands.getHandCategory().equals("3")){
               /* gyConsumptionDetails.setBarBillsId(blist.get(0).getID());*/
                /*yqgConsumptionDetailsService.insertYqgConsumptionDetails(gyConsumptionDetails);*/
            }
               /* if(yuzi[i].equals("1")){

                }
                else if(yuzi[i].equals("0")&&ertong[i].equals("1")){

                }
                else{

                }*/
               /* if(yqgHands.getHandCategory().equals("2")){
                    YqgConsumptionDetails YCD=new YqgConsumptionDetails();
                    LjwYqgBingbaiController lybc=new LjwYqgBingbaiController();
                    YCD.setMergeHands(mastercard);
                    YCD.setHandBrand(yqgHands.getHandBrand());
                    System.out.println(YCD.getMergeHands());
                    System.out.println(YCD.getHandBrand());
                    System.out.println(YCD);
                    lybc.bingpai(YCD);
                }*/


        }
        return new AjaxResult(200,"开牌成功");
    }

    /**
     * 修改吧台账单
     */
   /* @RequiresPermissions("project:whd:kaipai:edit")*/
    @Log(title = "吧台账单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YqgBarBills yqgBarBills)
    {
        return toAjax(yqgBarBillsService.updateYqgBarBills(yqgBarBills));
    }

    /**
     * 删除吧台账单
     */
  /*  @RequiresPermissions("project:whd:kaipai:remove")*/
    @Log(title = "吧台账单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs)
    {
        return toAjax(yqgBarBillsService.deleteYqgBarBillsByIDs(IDs));
    }
    public  int   sendmessage(String hand){
        Socket socket=new Socket();

        /** 构造方法(初始化客户端) */

            try {
                System.out.println("正在启动客户端...");
                socket = new Socket();
                socket.connect(new InetSocketAddress("169.254.170.253",6668),3000);
                System.out.println("客户端启动完毕!");
                /** 客户端开始工作的方法 */

                try {
                    // 返回此套接字的输出流
                    OutputStream out = socket.getOutputStream();
                    // 将此字符串发送到服务端(用高级流)
                    OutputStreamWriter osw = new OutputStreamWriter(out,"UTF-8");
                    BufferedWriter bw = new BufferedWriter(osw);
                    PrintWriter pw = new PrintWriter(bw,true); // 自动行刷新

                    String sendInfo = ":310202,"+hand+".";//解锁命令*
                    /*String sendInfo = ":210201,"+hand+".";*///开牌命令
                    pw.println(sendInfo);


                } catch (Exception e) {
                    e.printStackTrace();
                }
                return 1;

            } catch (Exception e) {

                System.out.println("握手失败！");
                return 0;

            }finally {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }




    }
}
